<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise发送AJAX</title>
</head>
    <button>AJAX</button>

    <script>
        //获取按钮
        let btn = document.querySelector('button');
        //绑定事件
        btn.onclick = function(){
            let p = new Promise((resolve, reject) => {
                let xhr = new XMLHttpRequest();
                xhr.responseType = 'json';
                xhr.open('get','https://v1.hitokoto.cn/x');
                xhr.send();
                xhr.onload = function(){
                    if(xhr.status >= 200 && xhr.status < 300){
                        resolve({
                            status: xhr.status,
                            headers: xhr.getAllResponseHeaders(),
                            data: xhr.response
                        });
                    }else{
                        reject({
                            status: xhr.status,
                            headers: xhr.getAllResponseHeaders(),
                            data: xhr.response
                        })
                    }
                }
            });
            
            p.then(response => {
                console.log(response);
            }, error => {
                console.log(error);
            })
        }
    </script>
<body>
</body>
</html>